<?php

    class Style_m extends Model {

        function Style_m () {
            parent::Model();            
        }

        public function kreiraj_klase_stilova($name, $style)  /* return:TRUE or FALSE */
        {  
            /*CREATE A STYLE*/

            //$this->db->insert('style',  array('name' => $name, 'id_txt'=>'music' ));  
            $this->db->insert('stylecustom',  array('name' => $name, 'account_id'=> $this->session->userdata('id') ));  
            $style_id = $this->db->insert_id();


            /*CREATE A STYLE CLASS NODE*/
            foreach ($style as $node=>$value){                
                //$this->db->insert('styleclass',  array('name' => $node, 'style_id' => $style_id ));
                $this->db->insert('styleclass',  array('name' => $node, 'stylecustom_id' => $style_id ));

                $style_class_id = $this->db->insert_id();
                foreach ($value as $property=>$val){  

                    $feature = array(
                    'property' => $property, 
                    'value' => $val, 
                    'styleclass_id' => $style_class_id 
                    );

                    $this->db->insert('styleclassfeatures', $feature);    
                }
            }   


            return 1;
        }

        public function citaj_klase_stilova($style_default_custom,$encode=TRUE){ /*PRIMA ARRAY*/
            $style = array();

            /*READ STYLE CLASS*/
            $styleclass = $this->db->get_where('styleclass', $style_default_custom)->result();
            foreach($styleclass as $rowstyle){

                /*CREATE STYLE CLASS FEATURES*/
                $styleclassfeatures = $this->db->get_where('styleclassfeatures', array('styleclass_id'=> $rowstyle->id))->result();

                $feature=array();
                foreach($styleclassfeatures as $rowfeature){
                    $feature[$rowfeature->property] = $rowfeature->value;           
                }

                $style[$rowstyle->name] = $feature;
            }
            if($encode){
                echo json_encode(array('style'=>$style));    
            }else{
                return $style;
            }

        }  

        public function uredi_klase_stilova($style_id, $name, $style){   /* return:TRUE or FALSE */ 

            /*EDIT STYLE NAME*/
            //$this->db->where(array('id'=>$style_id))->update('style', array('name' => $name ));
            $this->db->where(array('id'=>$style_id))->update('stylecustom', array('name' => $name ));

            /*GET STYLE CLASS ID*/
            //$styleclass = $this->db->get_where('styleclass', array('style_id'=> $style_id))->result();
            $styleclass = $this->db->get_where('styleclass', array('stylecustom_id'=> $style_id))->result();
            foreach($styleclass as $rowstyle){ 
                $node_id = $rowstyle-> id;
                $node_name = $rowstyle-> name;
                foreach($style[$node_name] as $property=>$val){
                    $this->db->where(array('property'=>$property, 'styleclass_id'=>$node_id))->update('styleclassfeatures', array('value' => $val ));

                }
            }

            return 1;

        }

        public function default_stil(){
            $style =array(
            "pollbox"=>array (
            "background-color"=> "#FAFAFA",
            "background-image"=> "none",
            "background-repeat"=> "no-repeat",
            "background-position"=> "left top",
            "border-width"=> "1px",
            "border-style"=> "solid",
            "border-color"=> "#DEDEDE",
            "border-radius"=> "12px",
            "margin-top"=> "0px",
            "margin-right"=> "0px",
            "margin-bottom"=> "0px",
            "margin-left"=> "0px",
            "padding-top"=> "12px",
            "padding-right"=> "12px",
            "padding-bottom"=> "12px",
            "padding-left"=> "12px",
            "width"=> "300px"     
            ),
            "question"=> array(
            "font-family"=> "Arial",
            "color"=> "#333333",
            "font-size"=> "15px",
            "font-weight"=> "bold",
            "font-style"=> "italic",
            "line-height"=> "20px",
            "text-align"=> "center",
            "text-decoration"=> "underline",
            "background-color"=> "none",
            "background-image"=> "none",
            "background-repeat"=> "no-repeat",
            "background-position"=> "center",
            "border-width"=> "0px",
            "border-style"=> "none",
            "border-color"=> "#333333",
            "border-radius"=> "0",
            "margin-top"=> "0px",
            "margin-right"=> "0px",
            "margin-bottom"=> "15px",
            "margin-left"=> "0px",
            "padding-top"=> "0px",
            "padding-right"=> "0px",
            "padding-bottom"=> "0px",
            "padding-left"=> "0px"       
            ),
            "answergroup"=> array(
            "padding-top"=> "2px",
            "padding-right"=> "0px",
            "padding-bottom"=> "2px",
            "padding-left"=> "0px"       
            ),
            "answercheck"=> array(
            "padding-top"=> "0px",
            "padding-right"=> "0px",
            "padding-bottom"=> "0px",
            "padding-left"=> "0px"       
            ),
            "answers"=> array(
            "font-family"=> "Arial",
            "color"=> "#333333",
            "font-size"=> "13px",
            "font-weight"=> "normal",
            "font-style"=> "normal",
            "line-height"=> "16px",
            "text-align"=> "left",
            "text-decoration"=> "none",            
            "padding-top"=> "0px",
            "padding-right"=> "0px",
            "padding-bottom"=> "0px",
            "padding-left"=> "0px",
            "width"=> "260px"       
            ),
            "otherinput"=> array(
            "font-family"=> "Arial",
            "color"=> "#333333",
            "font-size"=> "12px",
            "font-weight"=> "normal",
            "font-style"=> "normal",
            "line-height"=> "12px",
            "text-align"=> "left",
            "text-decoration"=> "none",
            "background-color"=> "none",
            "background-image"=> "none",
            "background-repeat"=> "no-repeat",
            "background-position"=> "center",
            "border-width"=> "1px",
            "border-style"=> "solid",
            "border-color"=> "#D1D1D1",
            "border-radius"=> "0px",
            "margin-top"=> "4px",
            "margin-right"=> "0px",
            "margin-bottom"=> "0px",
            "margin-left"=> "25px",
            "padding-top"=> "2px",
            "padding-right"=> "2px",
            "padding-bottom"=> "2px",
            "padding-left"=> "2px",       
            "width"=> "200px"       
            ),
            "votebutton"=> array(
            "font-family"=> "Arial",
            "color"=> "#333333",
            "font-size"=> "14px",
            "font-weight"=> "bold",
            "font-style"=> "normal",
            "line-height"=> "24px",
            "text-align"=> "left",
            "text-decoration"=> "none",
            "background-color"=> "#F2F2F2",
            "background-image"=> "url(".base_url()."assets/img/backgrounds/but.png)",
            "background-repeat"=> "repeat-x",
            "background-position"=> "left top",
            "border-width"=> "1px",
            "border-style"=> "solid",
            "border-color"=> "#999999",
            "border-radius"=> "12px",
            "margin-top"=> "0px",
            "margin-right"=> "0px",
            "margin-bottom"=> "0px",
            "margin-left"=> "0px",
            "padding-top"=> "0px",
            "padding-right"=> "24px",
            "padding-bottom"=> "0px",
            "padding-left"=> "24px",      
            "display"=> "block"                  
            ),
            "links"=> array(
            "font-family"=> "Arial",
            "color"=> "#333333",
            "font-size"=> "11px",
            "font-weight"=> "normal",
            "font-style"=> "normal",
            "line-height"=> "26px",
            "text-align"=> "left",
            "text-decoration"=> "underline",            
            "padding-top"=> "8px",
            "padding-right"=> "10px",
            "padding-bottom"=> "0px",
            "padding-left"=> "0px"                
            ),
            "resultsbackground"=> array(
            "background-color"=> "none",
            "background-image"=> "none",
            "background-repeat"=> "repeat-x",
            "background-position"=> "center",
            "border-width"=> "1px",
            "border-style"=> "solid",
            "border-color"=> "#D1D1D1",
            "border-radius"=> "10px",             
            "padding-top"=> "2px",
            "padding-right"=> "1px",
            "padding-bottom"=> "2px",
            "padding-left"=> "1px", 
            "margin-top"=> "4px",
            "margin-right"=> "0px",
            "margin-bottom"=> "18px",
            "margin-left"=> "0px"
            ),
            "resultsbar"=> array(
            "background-color"=> "#FF9900",
            "background-image"=> "none",
            "background-repeat"=> "repeat-x",
            "background-position"=> "center",
            "border-width"=> "1px",
            "border-style"=> "solid",
            "border-color"=> "#D1D1D1",
            "border-radius"=> "10px",             
            "padding-top"=> "4px",
            "padding-right"=> "1px",
            "padding-bottom"=> "4px",
            "padding-left"=> "1px", 
            "margin-top"=> "0px",
            "margin-right"=> "0px",
            "margin-bottom"=> "0px",
            "margin-left"=> "0px",   
            "height"=> "4px"   
            ),
            "totalvotes"=> array(
            "font-family"=> "Arial",
            "color"=> "#333333",
            "font-size"=> "11px",
            "font-weight"=> "normal",
            "font-style"=> "normal",
            "line-height"=> "26px",
            "text-align"=> "left",
            "text-decoration"=> "none",
            "background-color"=> "#none",
            "background-image"=> "none",
            "background-repeat"=> "repeat-x",
            "background-position"=> "center",
            "border-width"=> "0px",
            "border-style"=> "none",
            "border-color"=> "none",
            "border-radius"=> "0px",             
            "padding-top"=> "0px",
            "padding-right"=> "0px",
            "padding-bottom"=> "0px",
            "padding-left"=> "0px", 
            "margin-top"=> "0px",
            "margin-right"=> "0px",
            "margin-bottom"=> "0px",
            "margin-left"=> "0px"         
            ));

            /*CREATE A NEW STYLE*/
            /*if($this->style->createastyle($style)){
            $this->style->read_style();
            }else echo 'Error ocured';*/
        }


        public function lisaj_stilove(){            
            return $this->db->get_where('style')->result();
        }
        public function lisaj_podesene_stilove(){            
            //return $this->db->get_where('style')->result();
            return $this->db->get_where('stylecustom', array('account_id'=> $this->session->userdata('id')))->result();
        }  
        /*return style data || custom style data -> dependency $template=NULL*/      
        public function lisaj_podatke_stila($id,$template=NULL){            
            if($template!=NULL){
                return $this->db->get_where('style', array('id'=> $id))->result();
            //}else return $this->db->get_where('style', array('id'=> $id))->result();
            }else return $this->db->get_where('stylecustom', array('id'=> $id))->result();
        }


        public function brisi_podeseni_stil($id){
            $res = $this->db->delete('stylecustom', array('id' => $id));            
            $res = $this->db->delete('styleclass', array('stylecustom_id' => $id));            
            if($res){
                return true;
            }else return false;
        }
    }
?>